#include <stdio.h>
#include <stdlib.h>
#include "list.h"
#include "def.h"

typedef int datatype;

typedef struct kernel{
    int data;
    struct list_head my;
}kernel,*pklist;

pklist creat_head(){
    pklist head = (pklist)malloc(sizeof(kernel));
    if(head == NULL){
        printf("malloc err\n");
        return NULL;
    }
    INIT_LIST_HEAD(&(head->my));
    return head;
}

pklist creat_node(datatype data){
    pklist node=creat_head();
    if(node == NULL){
        printf("create head err\n");
        return NULL;
    }
    node->data=data;
    return node;
}

static void list_add(struct list_head* new,struct list_head* head){
    __list_add(new,head,head->next);
}

static void list_move(struct list_head* node,struct list_head* head){
    __list_del(node->prev,node->next);
    list_add(node,head);
}

static void list_move_tail(struct list_head* node,struct list_head* head){
    __list_del(node->prev,node->next);
    list_add_tail(node,head);
}

void insert_head(pklist head){
    int data =0;
    printf("请输入\n");
    scanf("%d",&data);
    pklist new = creat_node(data);
    list_add(&(new->my),&(head->my));
}

void show(const pklist head){
    struct list_head* pos=NULL;
    pklist node = NULL;
    list_for_each(pos,&(head->my)){
        node = list_entry(pos,struct kernel,my);
        printf("%d ",node->data);
    }
    printf("\n");

    printf("list_for_each_safe\n");
    struct list_head* safe_pos = NULL;
    list_for_each_safe(pos,safe_pos,&(head->my)){
        node = list_entry(pos,struct kernel,my);
        printf("%d ",node->data);
    }
    printf("\n");

    printf("list_for_each_entry\n");
    list_for_each_entry(node,&(head->my),my){
        printf("--%d",node->data);
    }
    printf("\n");

    printf("list_for_each_entry_safe\n");
    pklist safe_node =NULL;
    list_for_each_entry_safe(node,safe_node,&(head->my),my){
        printf("--%d,%d",node->data,safe_node->data);
    }
}

void delete_node(pklist head){
    datatype data=0;
    printf("删除的值\n");
    scanf("%d",&data);

    pklist pos =NULL;
    list_for_each_entry(pos,&(head->my),my){
        if(pos->data == data){
            break;
        }
    }
    if(pos != head){
        list_del(&(pos->my));
        free(pos);
        pos=NULL;
    }


}

pklist find_node(const pklist head,const pklist node){
    pklist pos=NULL;
    list_for_each_entry(pos,&(head->my),my){
        if(pos->data == node->data){
            break;
        }
    }
    if(pos != head){
        printf("找到了\n");
        return pos;
    }
    return NULL;
}

void updata_node(pklist head){
    datatype data = 0;
    datatype newdata =0;
    printf("请输入要修改的值\n");
    scanf("%d",&data);
    printf("请输入新的值\n");
    scanf("%d",&newdata);

    pklist node = creat_node(data);
    pklist pos =find_node(head,node);
    if(pos != NULL){
        pos->data=newdata;
    }
    free(node);
    node = NULL;
}

void move_node(pklist head){
    datatype data=0;
    int move_to=0;
    printf("请输入要移动的值\n");
    scanf("%d",&data);

    pklist data_node = creat_node(data);
    pklist pos = find_node(head,data_node);
    free(data_node);
    data_node = NULL;

    if(pos == NULL){
        return;
    }

    printf("请输入要移动到的位置:\n");
    scanf("%d",&move_to);
    data_node=creat_node(move_to);
    pklist move_to_pos =find_node(head,data_node);
    free(data_node);
    data_node=NULL;

    if(move_to_pos == NULL){
        return;
    }

    list_move_tail(&(pos->my),&(move_to_pos->my));
}


int main(void){
    pklist head = creat_head();
    printf("head:%p\n",head);
    printf("head->my:%p\n",&(head->my));

    unsigned long offset =(unsigned long)(&((kernel*)0)->my);
    printf("offset = %ld\n",offset);

    kernel*p=(kernel*)((char*)(&(p->my))-offset);
    printf("%p\n",p);

    kernel* p1=list_entry(&(head->my),kernel,my);
    printf("%p\n",p);

    for(int i=0;i<5;i++){
        insert_head(head);
    }
    show(head);


    delete_node(head);
    show(head);

    updata_node(head);
    show(head);

    move_node(head);
    show(head);


    return 0;
}